package com.j.lemon.learn.jdbc;

import cn.hutool.db.ds.druid.DruidDSFactory;
import cn.hutool.setting.Setting;
import lombok.Builder;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author lijunjun
 * @Date 2020-07-08 14:09
 * @Description
 */
public class DataSourceUtils {
    private static final Logger logger = LoggerFactory.getLogger(DataSourceUtils.class);
    private static final String URL = "jdbc:mysql://%s/%s?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull&useSSL=false";
    private static final ConcurrentHashMap<String, DataSource> POOL = new ConcurrentHashMap<>();


    public static Connection getConnection(String dbCode, String ipPort, String db, String username, String password) throws Exception {
        if(POOL.get(dbCode)==null){
            synchronized (DataSourceUtils.class){
                if(POOL.get(dbCode)==null){
                    Setting setting = new Setting();
                    String url = String.format(URL, ipPort, db);
                    setting.set("url",url);
                    setting.set("username",username);
                    setting.set("password",password);
                    setting.set("driverClassName","com.mysql.jdbc.Driver");
//                    setting.set("maxActive","");
//                    setting.set("maxIdle","");
//                    setting.set("minIdle","");
//                    setting.set("minIdle","");
//                    setting.set("initialSize","");
//                    setting.set("maxWait","");
                    DataSource dataSource = DruidDSFactory.create(setting).getDataSource();
                    POOL.put(dbCode,dataSource);
                }
            }
        }
        return POOL.get(dbCode).getConnection();
    }




    public static DataSource getDatasource(DbProperties dbProperties) throws SQLException {
        String urlPrefix = null;
        String url = null;
        DbTypeEnum type = dbProperties.getType();
        String ip = dbProperties.getIp();
        String port = dbProperties.getPort();
        String database = dbProperties.getDatabase();
        String nodes = dbProperties.getNodes();
        String sid = dbProperties.getSid();
        String serviceName = dbProperties.getServiceName();
        if (type == DbTypeEnum.mysql) {
            urlPrefix = DbConstant.URL_PREFIX.MYSQL;
            url = ip + ":" + port + "/" + database;
        } else if (type == DbTypeEnum.goldendb) {
            urlPrefix = DbConstant.URL_PREFIX.GOLDENDB;
            if(nodes != null){
                urlPrefix = DbConstant.URL_PREFIX.GOLDENDB_LB;
                url = nodes + "/" + database;
            }else{
                url = ip + ":" + port + "/" + database;
            }
        } else if (type == DbTypeEnum.oracle || type == DbTypeEnum.oracle12c) {
            urlPrefix = DbConstant.URL_PREFIX.ORACLE;
            if (StringUtils.isEmpty(sid)) {
                //old ipv6下会报错
                url = ip + ":" + port + "/" + serviceName;
                //20221124 new 兼容ipv4和ipv6
                //url = ip + ":" + port + ":" + serviceName;
            } else {
                url = ip + ":" + port + ":" + sid;
            }
        } else if (type == DbTypeEnum.oceanbase) {
            urlPrefix = DbConstant.URL_PREFIX.OCEANBASE;
            url = ip + ":" + port + "/" + database;
        } else if (type == DbTypeEnum.postgresql) {
            urlPrefix = DbConstant.URL_PREFIX.POSTGRESQL;
            url = ip + ":" + port + "/" + database;
        } else if (type == DbTypeEnum.gbase || type == DbTypeEnum.gbase8s) {
            urlPrefix = DbConstant.URL_PREFIX.GBASE;
            url = ip + ":" + port + "/" + database + ":GBASEDBTSERVER=" + serviceName;
        }else if(type == DbTypeEnum.antdb){
            urlPrefix = DbConstant.URL_PREFIX.POSTGRESQL;
            url = nodes != null ? nodes : ip + ":" + port;
            url += "/" + database;
        }

        Setting setting = new Setting();
        setting.set("url",urlPrefix + url);
        setting.set("username",dbProperties.getUsername());
        setting.set("password",dbProperties.getPassword());
        setting.set("driverClassName",dbProperties.getDriverClassName());
        return DruidDSFactory.create(setting).getDataSource();
    }

    public static void main(String[] args) {
        boolean matches = "test".matches("[0-9,a-A]");
        System.out.println(matches);
    }

//    public static void main(String args[]) {
//        String str = "qwe";
//        String pattern = "[0-9,a-A]";
//
//        Pattern r = Pattern.compile(pattern);
//        Matcher m = r.matcher(str);
//        System.out.println(m.matches());
//    }


}
